package handlers

import (
	"encoding/json"
	"fmt"
	"net/http"

	"v8s/compose/model"

	"github.com/labstack/echo/v4"
)

type (
	handler struct {
		schema []byte
	}
)

func CreateHandler() *handler {
	schema := model.Schema()
	marshalled, err := json.Marshal(schema)
	if err != nil {
		panic(fmt.Errorf("failed to marshall schema: %w", err))
	}

	return &handler{
		schema: marshalled,
	}
}

func (h *handler) GetComposeSchema(ctx echo.Context) error {
	return ctx.Blob(http.StatusOK, echo.MIMEApplicationJSONCharsetUTF8, h.schema)
}
